package com.gang.music.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gang.music.common.AjaxResult;
import com.gang.music.common.TreeNode;
import com.gang.music.system.entity.Menu;
import com.gang.music.system.service.MenuService;
import com.gang.music.system.service.RoleMenuService;
import com.smallchill.core.toolbox.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RoleMenuService roleMenuService;

	@RequestMapping("/list")
	public String menuList(Map<String, Object> map) {
		
		map.put("menuList", menuService.findForTreeTable(0));
		return "admin/menu/menuList";
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String menuInput() {
		
		return "admin/menu/menuInput";
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResult saveOrUpdate(Menu menu) {
		
		try {
			if(Func.isEmpty(menu.getId()))
				menuService.insert(menu);
			else
				menuService.updateById(menu);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}
	
	@RequestMapping("/update/{id}")
	public String update(@PathVariable Integer id, 
			Map<String, Object> map) {
		
		if(!Func.isEmpty(id)) {
			map.put("menu", menuService.selectByIdWithParent(id));
		}
		return "admin/menu/menuInput";
	}
	
	@ResponseBody
	@RequestMapping("/menuList")
	public AjaxResult getMenuList(@RequestParam(value="id", required=false)
		Integer roleId, @RequestParam(value="menuType", required=false) Integer type) {
		
		try {
			List<Menu> menuList = null;
			List<Integer> menuIds = null;
			List<TreeNode> treeNodes = new ArrayList<TreeNode>();
			
			EntityWrapper<Menu> ew = null;
			if(!Func.isEmpty(type)) {
				ew = new EntityWrapper<>();
				ew.eq("type", type);
			}
			menuList = menuService.selectList(ew);
			if(!Func.isEmpty(roleId)) {
				menuIds = roleMenuService.findMenuIdsByRoleId(roleId);
			}
			TreeNode node = null;
			for (Menu menu : menuList) {
				node = new TreeNode();
				node.setId(menu.getId()+"");
				node.setName(menu.getMenuName());
				node.setpId(menu.getParentId()+"");
				if(!Func.isEmpty(menuIds) 
						&& menuIds.contains(menu.getId())) {
					node.setChecked(true);
				}else {
					node.setChecked(false);
				};
				treeNodes.add(node);
			}
			return success(treeNodes);
		} catch (Exception e) {
			e.printStackTrace();
			return error("获取菜单列表出错");
		}
	}
	
	@ResponseBody
	@RequestMapping("/treeTable")
	public AjaxResult treeTable(Integer parentId) {
		
		if(!Func.isEmpty(parentId)) {
			try {
				List<Menu> menus = menuService.findForTreeTable(parentId);
				return success(menus);
			} catch (Exception e) {
				e.printStackTrace();
				return error("系统出错");
			}
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxResult delete(@PathVariable Integer id) {
		
		try {
			menuService.deleteById(id);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}
}
